package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.cacheable.Cacheable;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;

import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Member;
import com.itheima.pojo.Template;
import com.itheima.service.TemplateService;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("template")
public class TemplateController {

    @Reference(version = "1.0.0")
    private TemplateService templateService;

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = templateService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    @Cacheable(expire = 600)
    @GetMapping("findAll")
    public Result findAll(){
        List<Template> templates = templateService.findAll();
        System.out.println("这里是查询所有模板管理，我从数据库查询了！！！");
        return new Result(true,"查询成功",templates);
    }


    @RequestMapping("add")
    public Result add(@RequestBody Template template, Errors errors){
        if(errors.hasErrors()){
            StringBuilder msg = new StringBuilder();
            List<FieldError> fieldErrors = errors.getFieldErrors();
            for (int i = 0; i < fieldErrors.size(); i++) {
                if(i!=fieldErrors.size()-1) {
                    msg.append(fieldErrors.get(i).getDefaultMessage()).append(",");
                }else {
                    msg.append(fieldErrors.get(i).getDefaultMessage()).append("!");
                }
            }
            return new Result(false, msg.toString());
        }
        //调用业务层添加方法
        templateService.add(template);
        //返回操作结果
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }
    @GetMapping("findById/{id}")
    public Result findById(@PathVariable int id){
        Template template = templateService.findById(id);
        return new Result(true,"查询成功",template);
    }

      @GetMapping("findMember")
    public Result findMember( Long fileNumber){
        Member member = templateService.findMember(fileNumber);
        System.out.println("这里是查询所有模板管理，我从数据库查询了！！！");
        return new Result(true,"查询成功",member);

    }

    @RequestMapping("update")
    public Result update(@RequestBody Template template){
        //调用业务层修改方法
        templateService.update(template);
        //返回操作结果
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable int id){
        //调用业务层删除方法
        templateService.delete(id);
        //返回操作结果
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
}
